﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QueuesAndStacks.FloodFill
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[][] nums = new int[][]
            {   new int[]{0,0,0 },
                new int[]{0,1,1 },
                //new int[]{1,0,1 },
            };
            //int target = 3;
            FloodFill(nums, 1,1,1);
            //Console.WriteLine();
            Console.ReadKey();
        }
        public static int[][] FloodFill(int[][] image, int sr, int sc, int newColor)
        {
            //DFS
            int code = image[sr][sc];
            Flood(image, sr, sc, newColor, code);
            return image;
        }
        public static void Flood(int[][] image, int sr, int sc, int newColor, int code)
        {
            if (sr < 0 || sr >= image.Length || sc < 0 || sc >= image[sr].Length || image[sr][sc]!= code) return;

            if (image[sr][sc] == newColor) return;

            if (image[sr][sc] == code) image[sr][sc] = newColor;

            Flood(image, sr-1, sc, newColor, code);
            Flood(image, sr, sc-1, newColor, code);
            Flood(image, sr + 1, sc, newColor, code);
            Flood(image, sr, sc+1, newColor, code);
        }

    }
}
